package com.itheima.web.cookie;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 会话跟踪:Cookie
 */
@WebServlet("/aCookieServlet")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String value="张三";
        //使用URL进行对中文的编码
        //解码使用:URLDecoder.decode()
        value=URLEncoder.encode(value, "UTF-8");
        //创建Cookie对象,设置数据
        Cookie cookie = new Cookie("key", value);
        //设置cookie的存活时间(默认为秒)
        cookie.setMaxAge(60*60*12*7);//存活七天
        //发送Cookie到客户端:使用Cookie对象
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
